package debrasDevice;

import java.io.IOException;
import javax.jmdns.*;

public class TestRegister {
	// Display error message on failure
	public static void operationFailed(JmDNS jmdns, int errorCode) {
		System.out.println("Registration failed" + errorCode);
	}

	// Display registered name on success

	public static void serviceRegistered(JmDNS jmdns, ServiceInfo info) {
		System.out.println("\nRegistered Service as: " + info);
	}

	private static void testRegister(String name, int port)

	throws jmDNSException, IOException {
		JmDNS jmdns = JmDNS.create();
		System.out.println("Opened JmDNS");
		System.out
				.println("\nPress r and Enter, to register HTML service 'Media Server'");
		int b;
		while ((b = System.in.read()) != -1 && (char) b != 'r') {
			/* Stub */
		}
		ServiceInfo info = ServiceInfo.create("_http._tcp.local.", name, port,
				0, 0, "path=index.html");
		jmdns.registerService(info);
		serviceRegistered(jmdns, info);

		if (info == null) {
			operationFailed(jmdns, -1);
		}
	}

	public void baseChanged(BaseEvent pe) {
		System.out.println(" Base changed to: " + pe.getId());
	}

	public static void main(String[] args) throws jmDNSException, IOException {
		testRegister("Media Server", 1234);

	}

}
